//
//  LeftBarController.m
//  JPKY
//
//  Created by Vitas on 2016/11/13.
//  Copyright © 2016年 Vitas. All rights reserved.
//

#import "JPLeftBarController.h"
#import "JPSliderItemCell.h"
#import "JPSliderItemModel.h"
#import "JPSliderGroup.h"
#import "JPInformationViewController.h"
#import "JPUserProfileView.h"

static NSString *ReusedCellID = @"ReusedCellID";

@interface JPLeftBarController () <UITableViewDataSource, UITableViewDelegate>

/* 添加TableView */
@property (strong, nonatomic) UITableView *tableView;

@property(nonatomic,strong) NSMutableArray *groups;

@end

@implementation JPLeftBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 设置要显示的cell item信息
    [self setupItems];
    
    // 设置表头视图
    [self setupHeader];
    
}

#pragma mark - 设置表头视图和获取cell item信息

/**
 设置要显示的cell item信息
 */
- (void)setupItems {
    
    NSString *applyforDoctor = @"名医申请";
    NSString *illCaseManagement = @"病例管理";
    NSString *followedDoctors = @"关注医生";
    NSString *setting = @"设置与帮助";
    NSString *aboutProduct = @"关于产品";
    NSString *share = @"分享";
    
    JPSliderItemModel *item1 = [JPSliderItemModel itemWithTitle:applyforDoctor icon:@"More_LotteryRecommend"];
    
    JPSliderItemModel *item2 = [JPSliderItemModel itemWithTitle:illCaseManagement icon:@"MoreNetease"];
    JPSliderItemModel *item3 = [JPSliderItemModel itemWithTitle:followedDoctors icon:@"RedeemCode"];
    
    JPSliderGroup *group1 = [JPSliderGroup groupWithItems:@[item1, item2, item3]];
    
    JPSliderItemModel *item4 = [JPSliderItemModel itemWithTitle:setting icon:@"MoreHelp"];
    JPSliderItemModel *item5 = [JPSliderItemModel itemWithTitle:aboutProduct icon:@"MoreAbout"];
    JPSliderItemModel *item6 = [JPSliderItemModel itemWithTitle:share icon:@"MoreShare"];
    
    JPSliderGroup *group2 = [JPSliderGroup groupWithItems:@[item4, item5, item6]];
    
    self.groups = [NSMutableArray arrayWithObjects:group1, group2, nil];
}


/**
 设置表头视图
 */
- (void)setupHeader {
    
    // 表头视图高度
    CGFloat height = 100.0;
    
    UIView *headView = [[UIView alloc] init];
    headView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width *3.0/4.0f, height) ;
    headView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = headView;
    
    //添加tableView
    [self.view addSubview:self.tableView];
    
    // 添加头像view
    JPUserProfileView *userProfileView = [[JPUserProfileView alloc] init];
    
    userProfileView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 100);
    
    // 点击手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickUserView:)];
    
    [userProfileView addGestureRecognizer:tapGesture];
    
    [self.view addSubview:userProfileView];
    
}

-(void)viewDidAppear:(BOOL)animated{

}

#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    JPSliderGroup *group = self.groups[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JPSliderItemCell *cell = [tableView dequeueReusableCellWithIdentifier:ReusedCellID];
    
    if (cell == nil) {
        cell = [[JPSliderItemCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ReusedCellID];
    }

    JPSliderGroup *group = self.groups[indexPath.section];
    JPSliderItemModel *item = group.items[indexPath.row];
    
    cell.item = item;
    
    return cell;
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            [self didClickItemCell:@"JPApplyForDoctorVC" title:@"名医申请"];
        } else if (indexPath.row == 1) {
            [self didClickItemCell:@"JPCaseManagementVC" title:@"病例管理"];
        } else if (indexPath.row == 2) {
            [self didClickItemCell:@"JPConcernDoctorVC" title:@"关注医生"];
        }
    }
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        [self didClickItemCell:@"JPSettingVC" title:@"设置与帮助"];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/**
 *  点击了cell  跳转到相应界面
 */
- (void)didClickItemCell:(NSString *)ClassName title:(NSString *)title {
    
    Class clsVC = NSClassFromString(ClassName);

    UIViewController *vc = [[clsVC alloc] init];
    vc.title = title;
    UITabBarController *tab = (UITabBarController *)self.drawer.centerViewController;
    UINavigationController *nav = tab.selectedViewController;
    [nav popViewControllerAnimated:NO];
    [self.drawer close];
    [nav pushViewController:vc animated:NO];

}

/**
 *  点击了病例管理cell  跳转到相应界面
 */
- (void)didClickCaseItemCell:(NSString *)ClassName {
    
    Class clsVC = NSClassFromString(ClassName);
    
    UIViewController *vc = [[clsVC alloc] init];
    vc.title = @"病例管理";
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self];
    [nav popViewControllerAnimated:NO];
    [self.drawer close];
    [nav pushViewController:vc animated:NO];
    
}


#pragma mark - 用户信息设置点击响应事件
/**
 *  点击了头像的View  跳转到用户信息设置界面
 */
- (void)didClickUserView:(UIView *)view{
    
    //创建目标控制器
    JPInformationViewController  *loginController = [[JPInformationViewController alloc] init];
    UITabBarController *tab = (UITabBarController *)self.drawer.centerViewController;
    UINavigationController *nav = tab.selectedViewController;
    [nav popViewControllerAnimated:NO];
    [self.drawer close];
    [nav pushViewController:loginController animated:NO];
    
}




#pragma mark - 懒加载tableView
- (UITableView *)tableView{
    
    if (!_tableView ) {
        
        CGRect frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width*3.0/4.0f, self.view.bounds.size.height);
        _tableView = [[UITableView alloc] initWithFrame: frame style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        // 注册cell
        [_tableView registerClass:[JPSliderItemCell class] forCellReuseIdentifier:ReusedCellID];
    }
    
    return _tableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
